package com.wtb.javatool.service.impl;

import com.wtb.javatool.dao.AdministratorsMapper;
import com.wtb.javatool.dao.DatabaseTableOwnershipMapper;
import com.wtb.javatool.dao.PublicDatabaseMapper;
import com.wtb.javatool.dao.TableMetaMapper;
import com.wtb.javatool.entity.*;
import com.wtb.javatool.service.IPublicDatabaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PublicDatabaseService implements IPublicDatabaseService {

    @Autowired
    private PublicDatabaseMapper publicDatabaseMapper;
    @Autowired
    private AdministratorsMapper administratorsMapper;
    @Autowired
    private DatabaseTableOwnershipMapper databaseTableOwnershipMapper;
    @Autowired
    private TableMetaMapper tableMetaMapper;

    public Integer application(PublicDatabase publicDatabase){
        //查询数据表对应的服务器序号
        DatabaseTableOwnership databaseTableOwnership = databaseTableOwnershipMapper.selectSever(publicDatabase.getDatabaseName());
        //判断是否为空
        if (databaseTableOwnership == null) {
            System.out.println("databaseTableOwnership is null");
            return 0;
        }
        int severNumber = databaseTableOwnership.getSeverNumber();
        String sever;
        switch (severNumber){
            case 1:
                sever = "student_experimental_database";
                break;
            case 2:
                sever = "original_database";
                break;
            case 3:
                sever = "test_database";
                break;
            case 4:
                sever = "production_database";
                break;
            case 5:
                sever = "development_database";
                break;
            default:
                return 0;
        }
        //查询有对应权限的管理员
        Administrators administrators = administratorsMapper.selectAdministratorsNameOption(sever);
        //测试是否为空
        if(administrators == null){
            System.out.println("administrators is null");
            System.out.println(administrators.getAdministratorName());
            return 0;
        }
        //将管理员ID存入申请表对应对象中
        publicDatabase.setAuthorizerId(administrators.getAdministratorName());
        return publicDatabaseMapper.application(publicDatabase);
    }

    public List<PublicDatabase> query(){
        return publicDatabaseMapper.query();
    }

    public List<TableMeta> tableMeta(){
        List<TableMeta> result = tableMetaMapper.tableMeta();
        System.out.println("MyBatis查询结果: " + result); // 检查是否为null或属性未填充
        return tableMetaMapper.tableMeta();
    }
}
